﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TransactionsEmulator
{
    public partial class Form1 : Form
    {
        private List<uint> rates;
        private List<uint> banks;
        public MySqlConnection conn = null;
        private string connStr = null;
        private DataTable ds = null;
        private string[] queries = {//Fill main grid
                                    @"SELECT Currency_ID,
                                             Name
                                     FROM currencies"
                                   };




        public Form1()
        {
            InitializeComponent();
            rates = new List<uint>();
            banks = new List<uint>();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Interval = 100;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            connStr = "server=localhost;userid=root;password=test;database=test";
            conn = new MySqlConnection(connStr);
            ds = new DataTable();
            try
            {
                conn.Open();
                string queryGetRates = "SELECT Rate_ID FROM rates";
                string queryGetBanks = "SELECT Bank_ID FROM banks";
                MySqlCommand cmd = new MySqlCommand(queryGetRates, conn);
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        rates.Add(reader.GetUInt32("Rate_ID"));
                    }
                }
                cmd = new MySqlCommand(queryGetBanks, conn);
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        banks.Add(reader.GetUInt32("Bank_ID"));
                    }
                }
                using (MySqlDataAdapter da = new MySqlDataAdapter(queries[0], conn))
                {
                    da.Fill(ds);
                    listBox1.DataSource = ds;
                    listBox2.DataSource = ds.Copy();
                    listBox1.DisplayMember =  listBox2.DisplayMember = "Name";
                    listBox1.ValueMember = listBox2.ValueMember = "Country_ID";
                }
            }
            catch (MySqlException exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random rand = new Random((int)DateTime.Now.Ticks);
            string query = @"INSERT INTO transactions (Time, Date, Quantity, Rate_ID, Bank_ID, Type)
                            VALUES ('{0}','{1}','{2}','{3}','{4}', '{5}')";
            MySqlCommand cmd;
            for (int i = 0; i < 100; i++)
            {
                uint rate = rates[rand.Next(rates.Count)];
                uint bank = banks[rand.Next(banks.Count)];
                cmd = new MySqlCommand(String.Format(query, DateTime.Now.AddMinutes(rand.Next(200)).ToString("HH:mm:ss"), DateTime.Now.Date.AddDays(rand.Next(5)).ToString("yyyy-MM-dd"), rand.Next(100), rate, bank,rand.Next(0,1)), conn);
                cmd.ExecuteNonQuery();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            /*
            List<uint> currencies = new List<uint>();
            string query = "SELECT Currency_ID FROM currencies";
            MySqlCommand cmd = new MySqlCommand(query, conn);
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    currencies.Add(reader.GetUInt32("Currency_ID"));
                }
            }
            Random rand = new Random((int)DateTime.Now.Ticks);
            string query2 = @"INSERT INTO rates (Buy, Sell, Date, F_Currency_ID, S_Currency_ID)
                            VALUES ('{0}','{1}','{2}','{3}','{4}')";
            for (int i = 0; i < 10; i++)
            {
                uint currency = currencies[rand.Next(currencies.Count)];
                string buy = (rand.NextDouble() * 100).ToString();
                string sell = (Convert.ToDouble(buy) + rand.NextDouble()).ToString();
                int sign = (rand.Next(1) == 0)? 1:-1;
                string qry = String.Format(query2, buy.Replace(",", "."), sell.Replace(",", "."), DateTime.Now.Date.AddDays(sign*rand.Next(10)).ToString("yyyy-MM-dd"), currencies[rand.Next(currencies.Count)], currency);
                cmd = new MySqlCommand(qry,conn);
                cmd.ExecuteNonQuery();
            }
            */
            MySqlCommand cmd;
            Random rand = new Random((int)DateTime.Now.Ticks);
            string query2 = @"INSERT INTO rates (Buy, Sell, Date, F_Currency_ID, S_Currency_ID)
                            VALUES ('{0}','{1}','{2}','{3}','{4}')";

            DateTime dateFrom = dateTPFrom.Value;
            DateTime dateTo = dateTPTo.Value;

            int delta = (dateTo - dateFrom).Days + 1;
            for (int i = 0; i < delta; i++)
            {
                dateFrom = dateFrom.AddDays(1);
                foreach (DataRowView sel in listBox1.CheckedItems)
                {
                    int firstIndex = (int)sel.Row.ItemArray[0];
                    foreach (DataRowView item in listBox2.CheckedItems)
                    {
                        int secondIndex = (int)item.Row.ItemArray[0];
                        decimal buy = (decimal)(rand.Next(100) + rand.NextDouble());
                        decimal sell = (decimal)((float)buy + rand.Next(1, 4) + rand.NextDouble());
                        string qry = String.Format(query2, buy.ToString().Replace(",", "."), sell.ToString().Replace(",", "."), dateFrom.Date.ToString("yyyy-MM-dd"), firstIndex, secondIndex);
                        cmd = new MySqlCommand(qry, conn);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            conn.Close();
        }
    }
}
